package com.namecard.namecardapi.controller;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.namecard.namecardapi.entity.TCate;
import com.namecard.namecardapi.entity.TCommodity;
import com.namecard.namecardapi.entity.vo.AddTCateVo;
import com.namecard.namecardapi.entity.vo.SaveTCateVo;
import com.namecard.namecardapi.entity.vo.TCateQueryVo;
import com.namecard.namecardapi.result.Result;
import com.namecard.namecardapi.service.TCateService;
import com.namecard.namecardapi.service.TCommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjl
 * @since 2024-04-21
 */
@DS("slave2")
@Api(tags = "分类管理") //Swagger生成api的提示
@RestController
@RequestMapping("/t-cate")
public class TCateController {
    public static final String 每页记录数 = "每页记录数";
    @Resource
    private TCateService cateService;
    @Resource
    private TCommodityService commodityService;
    @ApiOperation(value = "分页查询")
    @GetMapping("/{page}/{limit}")
    public Result page(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = 每页记录数, required = true)
            @PathVariable Long limit,

            @ApiParam(name = "cateQueryVo", value = "查询对象", required = false)
            TCateQueryVo cateQueryVo) {
        Page<TCate> pageParam = new Page<>(page,limit);
        IPage<TCate> pageModel = cateService.selectPage(pageParam, cateQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 分类是否首页显示
     * @param cateId
     * @return
     */
    @ApiOperation(value = "类别是否首页显示")
    @GetMapping("/show/{cateId}")
    public Result show(@PathVariable("cateId") Long cateId){
        TCate cate = cateService.getById(cateId);
        if(cate.getIsShow()==0){
            cate.setIsShow(1);
        }else{
            cate.setIsShow(0);
        }
        cateService.updateById(cate);
        return Result.ok("");
    }

    //首页分类商品展示 给前端首页展示 详情页用另一个接口
    @ApiOperation(value = "类别显示的商品")
    @GetMapping("/show/commodity")
    public Result showCommodity(){
        QueryWrapper<TCate> wrapper = new QueryWrapper<>();
        wrapper.eq("is_show",1);
        List<TCate> list = cateService.list(wrapper);
        for (TCate cate : list) {
            QueryWrapper<TCommodity> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("cate_id", cate.getId()).last("LIMIT 10");
            List<TCommodity> list1 = commodityService.list(wrapper1);
            cate.setList(list1);
        }
        return Result.ok(list);

    }


    /**
     * 添加分类
     * @param cateVo
     * @return
     */
    @ApiOperation(value = "添加分类")
    @PostMapping("/save")
    public Result save(@RequestBody AddTCateVo cateVo) {
        TCate cate = new TCate();
        Date date = new Date();
        cate.setCreateTime(date);
        cate.setUpdateTime(date);
        cate.setCateName(cateVo.getCateName());
        boolean flag = cateService.save(cate);
        return flag ? Result.ok() : Result.fail();
    }

    /**
     * 根据id获取分类
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        TCate cate = cateService.getById(id);
        return Result.ok(cate);
    }

    /**
     * 修改分类
     * @param cateVo
     * @return
     */
    @ApiOperation(value = "根据id修改")
    @PutMapping("/update")
    public Result update(@RequestBody SaveTCateVo cateVo) {
        System.out.println(cateVo.getId());
        TCate cate = new TCate();
        cate.setUpdateTime(new Date());
        cate.setCateName(cateVo.getCateName());
        cate.setIsShow(cateVo.getIsShow());
        cate.setId(cateVo.getId());
        boolean flag = cateService.updateById(cate);
        return flag ? Result.ok() : Result.fail() ;
    }

    /**
     * 根据id批量删除
     * json的数组格式对应list集合
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/delete/{id}")

    public Result batch(@PathVariable Long id) {
        boolean flag = cateService.removeById(id);

        return flag ? Result.ok() : Result.fail();
    }



}

